/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAO.DonHangDAO;
import DAO.ProductCategoryDAO;
import DAO.ProductDAO;
import DAO.SupplierDAO;

import DAO.UserDAO;
import Pojo.LoaiSanPham;
import Pojo.NhaCungCap;
import Pojo.SanPham;
import Pojo.User;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author HDKhanh
 */
public class C_DonHang extends HttpServlet {

    final private String homePage = "index.jsp";
    final private String failLoginPage = "failLogin.jsp";
    final private String menuPage = "menuFunction.jsp";
    final private String qlndPage = "manageUser.jsp";
    final private String insert_updatePage = "insert_updateUser.jsp";
    final private String insert_updatePagePro = "insert_updatePro.jsp";
    final private String insert_updatePageCus = "insert_updateUserCus.jsp";
    final private String insert_updatePageProCate = "insert_updateProCate.jsp";
    final private String notAlowPage = "notAlow.jsp";
    final private String productPage = "manageProduct.jsp";
    final private String productCategoryPage = "manageProductCategory.jsp";
    final private String billPage = "manageBill.jsp";
    final private String customerPage = "manageCustomer.jsp";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //PrintWriter out=response.getWriter();
        try {
            //String action = request.getParameter("action");
            String action = request.getParameter("btAction");
            if (action == null) {
                RequestDispatcher rd = request.getRequestDispatcher("Controller?btAction=menuFunction");
                rd.forward(request, response);
            } else if (request.getParameter("username") == "") {
                RequestDispatcher rd = request.getRequestDispatcher(homePage);
                rd.forward(request, response);
            } else if (action.equals("qldh")) {
                User u = UserDAO.GetInfo(request.getParameter("username"));
                if (u.getLoai() == 3 || u.getLoai() == 4) {
                    DonHangDAO dh = new DonHangDAO();
                    List result = dh.View();
                    request.setAttribute("INFO", result);
                    String title = "Quản lý đơn hàng";
                    request.setAttribute("title", title);
                    RequestDispatcher rd = request.getRequestDispatcher(billPage);
                    rd.forward(request, response);
                } else {
                    RequestDispatcher rd = request.getRequestDispatcher(notAlowPage);
                    rd.forward(request, response);
                }
            } else if (action.equals("SearchBill")) {
                DonHangDAO dh = new DonHangDAO();
                List result = dh.Search(request.getParameter("txtSearch"));
                request.setAttribute("INFO", result);
                String title = "Quản lý đơn hàng";
                request.setAttribute("title", title);
                RequestDispatcher rd = request.getRequestDispatcher(billPage);
                rd.forward(request, response);
            } else if (action.equals("DeleteChiTietDonhang")) {
                DonHangDAO dh = new DonHangDAO();
                dh.DeleteChiTietDonHang(request.getParameter("mahd"), request.getParameter("masp"));
                String url = "C_Customer?btAction=DetailsCus&username=" + request.getParameter("username") + "&txtSearch=" + request.getParameter("txtSearch");
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            } else if (action.equals("DetailsBill")) {
                DonHangDAO dh = new DonHangDAO();
                List result = dh.ViewInfo(request.getParameter("mahd"));
                request.setAttribute("DETAILS", result);
                String url = "C_DonHang?btAction=SearchBill&txtSearch=" + request.getParameter("txtSearch");
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
